const minZIndex = 1050;

class ModalController {
  modalList = [];

  constructor() {}

  addModal(val) {
    this.modalList.push({
      ...val,
      uiArgs: {
        zIndex: this.getZIndex(),
      }
    });
  }

  getZIndex() {
    let zIndex = minZIndex;
    if (this.modalList.length > 0) {
      zIndex = this.modalList.at(-1).uiArgs.zIndex + 1;
    }

    return zIndex;
  }

  isFirstModal(modalType) {
    return this.modalList[0]?.modalType === modalType;
  }

  getModal(modalType) {
    return this.modalList.find(item => item.modalType === modalType);
  }

  deleteModal(modalType) {
    let delIndex = this.modalList.findIndex(item => item.modalType === modalType);
    if (delIndex !== -1) {
      this.modalList.splice(delIndex, 1);
    }
  }

  getModalList() {
    return this.modalList;
  }

  hasModal(modalType) {
    return this.modalList.some(item => item.modalType === modalType);
  }

  clearModalList() {
    this.modalList.splice(0, this.modalList.length);
  }

  dispose() {
    this.clearModalList();
  }
}

const modalController = new ModalController();

export {
  modalController,
};